package com.glory.brepda.activity.mfg;

import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.brepda.R;
import com.glory.brepda.adapter.MaterielLibraryListAdapter;
import com.glory.brepda.base.BaseActivity;
import com.glory.brepda.entity.MarerielLibraryBean;
import com.glory.brepda.entity.response.BaseResponseBean;
import com.glory.brepda.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralPop;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_LogUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_PopUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.OnClick;

public class MaterielLibraryActivity extends BaseActivity {


    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.etEqpWorkPosition)
    TextView etEqpWorkPosition;
    @BindView(R.id.tvType)
    TextView tvType;
    @BindView(R.id.etMaterielLot)
    FreeUI_ClearEditText etMaterielLot;
    @BindView(R.id.etLot)
    FreeUI_ClearEditText etLot;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.ivPositionArrow)
    ImageView ivPositionArrow;
    @BindView(R.id.ivTypePositionArrow)
    ImageView ivTypePositionArrow;
    @BindView(R.id.flPositionList)
    FrameLayout flPositionList;
    @BindView(R.id.flTypeList)
    FrameLayout flTypeList;
    @BindView(R.id.rvMaterielList)
    RecyclerView rvMaterielList;
    private List<String> mMaterielList;
    private List<String> mTypelList;
    private String type = "";
    private List<MarerielLibraryBean.MlotlistBean> mMaterielLibraryList = new ArrayList<>();
    private FreeUI_GeneralPop mMaterielPop;
    private MaterielLibraryListAdapter materielListAdapter;


    @Override
    protected int getLayoutResId() {
        return R.layout.activity_materiel_library;
    }

    @Override
    protected void initView() {

        String title = getIntent().getStringExtra(FreeApi_StaticMembers.SHOW_PLUGIN_KEY);
        tvTitle.setText(title);
        tvBaseSubmit.setVisibility(View.GONE);
        setMainLayout(llMain);
        mMaterielList = new ArrayList<>();
        mMaterielList.add("04001/一分厂线边仓");
        mMaterielList.add("04002/二分厂线边仓");
        mMaterielList.add("04003/三分厂线边仓");

        tvType.setText("物料");
        type = "物料";

        mTypelList = new ArrayList<>();
        mTypelList.add("物料");
        mTypelList.add("治具");
        //she fang xiang
        initRecyclerView(rvMaterielList,this);
        materielListAdapter = new MaterielLibraryListAdapter(null);
        materielListAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        materielListAdapter.isFirstOnly(true);
        rvMaterielList.setAdapter(materielListAdapter);
        initEditNotToUpperCase(etMaterielLot, s -> etLot.requestFocus());
        initEdit(etLot, s -> getMarerielLibraryInfo());
    }

    private void getMarerielLibraryInfo() {
        // 2023/12/18 应哥瑞利实施工程师要求注释，物料入库不强制要求输货架号，取消提交时需要输入货架号的卡控
//        if (etEqpWorkPosition.getText().toString().equals("") || getText(etMaterielLot).equals("")) {
        if (etEqpWorkPosition.getText().toString().equals("")) {
            showToast("填写未完成，请填写相关数据", false);
            return;
        }
        if (type.equals("")) {
            showToast("请选择类型", false);
            return;
        }

        FreeUI_EntityCallBack<BaseResponseBean<MarerielLibraryBean>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<MarerielLibraryBean>>
                (new TypeToken<BaseResponseBean<MarerielLibraryBean>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<MarerielLibraryBean>> response) {
                super.onSuccess(response);
                etLot.setText("");
                etLot.requestFocus();
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    MarerielLibraryBean body = response.body().getResponse().getBody();
                    mMaterielLibraryList.addAll(body.getMlotlist());
                    for (MarerielLibraryBean.MlotlistBean mlotlistBean : body.getMlotlist()) {
                        materielListAdapter.addData(0, mlotlistBean);
                    }
                }

            }

            @Override
            public void onError(Response<BaseResponseBean<MarerielLibraryBean>> response) {
                super.onError(response);
                loadError(response.getException(), "IF.ERPMLOTIN");
            }
        };
        JsonObject map = new JsonObject();

        map.addProperty("ACTION_TYPE", "IN");
        map.addProperty("MLOTID", getText(etLot));
        map.addProperty("TOWAREHOUSEID", etEqpWorkPosition.getText().toString().substring(0, 5));
        map.addProperty("TOSTORAGETYPE", "");
        map.addProperty("TOSTORAGEID", getText(etMaterielLot));
        map.addProperty("CLASS", type.equals("物料") ? "RAW" : "TOOL");
        NetUtils.requestPostNet("IF.ERPMLOTIN", "Common", MaterielLibraryActivity.this, map, callBack);

    }

    @Override
    protected void initData() {

    }

    @OnClick({R.id.tvBaseSubmit, R.id.flPositionList, R.id.flTypeList})
    public void onViewClicked(View v) {
        if (v.getId() == R.id.flPositionList) {
            mMaterielPop = FreeApi_PopUtils.getNormalPop(getApplicationContext(), flPositionList.getWidth(), mMaterielList, ivPositionArrow, (codeS, i) -> {
                etEqpWorkPosition.setText(codeS);
            });
            FreeApi_PopUtils.setArrowShow(ivPositionArrow);
            mMaterielPop.showAsDropDown(flPositionList, 0, 0);
        }

        if (v.getId() == R.id.flTypeList) {
            mMaterielPop = FreeApi_PopUtils.getNormalPop(getApplicationContext(), flTypeList.getWidth(), mTypelList, ivTypePositionArrow, (codeS, i) -> {
                tvType.setText(codeS);
                type = codeS;
            });
            FreeApi_PopUtils.setArrowShow(ivTypePositionArrow);
            mMaterielPop.showAsDropDown(flTypeList, 0, 0);
        }
    }


}

